package com.foreverwin.mes.so.controller;

import com.foreverwin.mes.common.constants.HandleBOConstants;
import com.foreverwin.mes.core.base.FrontPage;
import com.foreverwin.mes.core.base.AjaxResult;
import com.foreverwin.mes.core.utils.CommonMethods;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.foreverwin.mes.core.utils.StringUtils;
import com.foreverwin.mes.so.model.RecipeResrceItemRelView;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.foreverwin.mes.so.service.RecipeResrceItemRelService;
import com.foreverwin.mes.so.model.RecipeResrceItemRel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 *
 * @author Syngna
 * @since 2020-02-04
 */
@Controller
@RequestMapping("/recipeResrceItemRels")
public class RecipeResrceItemRelController {
    private final Logger logger = LoggerFactory.getLogger(RecipeResrceItemRelController.class);

    @Autowired
    public RecipeResrceItemRelService recipeResrceItemRelService;

    /**
    * 根据id查询
    *
    * @param id 主键
    * @return
    */
    @ResponseBody
    @GetMapping("/{id:.+}")
    public AjaxResult getRecipeResrceItemRelById(@PathVariable String id) {
        RecipeResrceItemRel result;
        try {
            result = recipeResrceItemRelService.selectById(id);
        } catch (Exception e) {
            logger.error("getRecipeResrceItemRelById -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 查询所有数据
     *
     * @return
     */
    @ResponseBody
    @GetMapping("")
    public AjaxResult getRecipeResrceItemRelList(RecipeResrceItemRel recipeResrceItemRel){
        List<RecipeResrceItemRel> result;
        try {
            EntityWrapper<RecipeResrceItemRel> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(recipeResrceItemRel);
            result = recipeResrceItemRelService.selectList(EntityWrapper);
        } catch (Exception e) {
            logger.error("getRecipeResrceItemRelList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 分页查询数据
     *
     * @param frontPage  分页信息
     * @return
     */
    @ResponseBody
    @GetMapping("/getRecipeResrceItemRelPageList")
    public AjaxResult getRecipeResrceItemRelPageList(FrontPage<RecipeResrceItemRel> frontPage, RecipeResrceItemRel recipeResrceItemRel){
        Page result;
        try {
            String site = CommonMethods.getSite();
            recipeResrceItemRel.setSite(site);
            EntityWrapper<RecipeResrceItemRel> EntityWrapper = new EntityWrapper<>();
            EntityWrapper.setEntity(recipeResrceItemRel);
            if (frontPage.getGlobalQuery() != null && !"".equals(frontPage.getGlobalQuery().trim())) {
                //TODO modify global query
                EntityWrapper
                    .like("handle", frontPage.getGlobalQuery())
                    .or().like("site", frontPage.getGlobalQuery())
                    .or().like("itemBo", frontPage.getGlobalQuery())
                    .or().like("resrce", frontPage.getGlobalQuery())
                    .or().like("recipeBo", frontPage.getGlobalQuery())
                    .or().like("item", frontPage.getGlobalQuery())
                    .or().like("itemVersion", frontPage.getGlobalQuery())
                    .or().like("creator", frontPage.getGlobalQuery())
                    .or().like("updater", frontPage.getGlobalQuery())
        ;
            }
            result = recipeResrceItemRelService.selectPage(frontPage.getPagePlus(), EntityWrapper);
        } catch (Exception e) {
            logger.error("getRecipeResrceItemRelPageList -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(result);
    }

    /**
     * 保存和修改公用
     * @param recipeResrceItemRel  传递的实体
     * @return  null 失败  实体成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_save")
    public AjaxResult recipeResrceItemRelSave(@RequestBody RecipeResrceItemRel recipeResrceItemRel) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        int count;
        try {
            count = recipeResrceItemRelService.insertOrUpdate(recipeResrceItemRel) ? 1 : 0;
        } catch (Exception e) {
            logger.error("recipeResrceItemRelSave -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        return AjaxResult.success(count == 0 ? null : recipeResrceItemRel);
    }

    /**
     * 根据id删除对象
     * @param id  实体ID
     * @return 0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id:.+}")
    public AjaxResult recipeResrceItemRelDelete(@PathVariable("id") String id){
        int count;
        String user = CommonMethods.getUser();
        try {
            count = recipeResrceItemRelService.deleteById(id) ? 1 : 0;
        } catch (Exception e) {
            logger.error("recipeResrceItemRelDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        logger.info("RECIPE_RELATION_OPERATION: 用户【{}】删除设备物料使用的配方关系【{}】成功!", user, id);
        return AjaxResult.success("count: " + count);
    }

    /**
     * 批量删除对象
     * @param ids 实体集合ID
     * @return  0 失败  1 成功
     */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST, value = "/_batchDelete")
    public AjaxResult removeByIds(@RequestBody List<String> ids){
        int count;
        String user = CommonMethods.getUser();
        try {
            count = recipeResrceItemRelService.deleteBatchIds(ids) ? 1 : 0;
        } catch (Exception e) {
            logger.error("recipeResrceItemRelBatchDelete -=- {}", e.toString());
            return AjaxResult.error( e.getMessage() );
        }
        logger.info("RECIPE_RELATION_OPERATION: 用户【{}】删除设备物料使用的配方关系【{}】成功!", user, ids);
        return AjaxResult.success("count: " + count);
    }

    @ResponseBody
    @GetMapping("addRelation")
    public AjaxResult addRelation(String recipe, String recipeVer, String item, String resrce) {
        String site = CommonMethods.getSite();
        String user = CommonMethods.getUser();
        String recipeBo = StringUtils.genHandle(HandleBOConstants.RECIPE_BO, site, recipe, recipeVer);
        String result;
        try {
            result = recipeResrceItemRelService.saveRelation(site, user, item, resrce, recipeBo);
        } catch (Exception e) {
            logger.error("addRecipeRelationData -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }

    @ResponseBody
    @GetMapping("findRelation")
    public AjaxResult findRelation( String item, String resrce) {
        List<RecipeResrceItemRelView> result;
        try {
            String site = CommonMethods.getSite();
            result = recipeResrceItemRelService.findRelation( site, resrce, item);
        } catch (Exception e) {
            logger.error("findRelation -=- {}", e.toString());
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(result);
    }
}